package com.example.eshop_ssm.service.impl;


import com.example.eshop_ssm.entity.TbBookInfo;
import com.example.eshop_ssm.entity.TbTypeInfo;
import com.example.eshop_ssm.mapper.BookManagerMapper;
import com.example.eshop_ssm.service.BookManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BookManagerServiceimpl implements BookManagerService {
    @Autowired
    private BookManagerMapper bookManagerMapper;

    @Override
    public Map<String, Object> selectBookInfo(String book_name, String book_author, String status,String type_id,String page) {
        Map<String, Object> selectMap = new HashMap<>();
        if (book_name!=null && book_name!=""){
            selectMap.put("book_name",book_name);
        }
        if (book_author!=null && book_author!=""){
            selectMap.put("book_author",book_author);
        }
        if (status!=null && status!=""){
            selectMap.put("status",Integer.parseInt(status));
        }
        if (type_id!=null && type_id!=""){
            selectMap.put("type_id",Integer.parseInt(type_id));
        }

        //分页---页数
        if ((page!=null && page!="")){
            if (Integer.parseInt(page)>1){
                selectMap.put("page",(Integer.parseInt(page)-1)*10);
            }else {
                selectMap.put("page",0);
            }
        }else {
            selectMap.put("page",0);
        }

        //总条数
        Integer totalPage = bookManagerMapper.selectCount(selectMap);
        //总页数
        Integer pageCount = totalPage%10==0? totalPage/10 : totalPage/10+1;

        //商品信息
        List<TbBookInfo>  bookInfo = bookManagerMapper.selectBookInfo(selectMap);

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("bookInfo",bookInfo); //书本信息
        objectMap.put("pageCount",pageCount); //记录总页数

        return objectMap;
    }

    @Override
    public List<TbTypeInfo> selectTypeId() {
        return bookManagerMapper.selectTypeId();
    }

    @Override
    public Integer deleteBook(Integer book_id) {
        return bookManagerMapper.deleteBook(book_id);
    }

    @Override
    public Integer updateBook(String control, String bookIds) {
        String[] s = control.split("-");// 拆出操作字段和控制变量
        String column = s[0];
        Integer controlVariables = Integer.parseInt( s[1] );

        HashMap<String, Object> updateMap = new HashMap<>();
        updateMap.put("column",column);
        updateMap.put("controlVariables",controlVariables);
        updateMap.put("bookIds",bookIds.split(","));

        return bookManagerMapper.updateBook(updateMap);
    }


}
